package filelisting

import (
	"io/ioutil"
	"net/http"
	"os"
	"strings"
)

const prefix = "/list/"

type userError string

func (e userError) Error() string{
	return e.Message()
}

func (e userError)  Message() string{
	return string(e)
}

func  HandleFileList(writer http.ResponseWriter,request *http.Request) error{
	if strings.Index(request.URL.Path,prefix) != 0{
		return userError("path must start " + "with "+prefix)
	}
	path:= request.URL.Path[len(prefix):]
	file,err := os.Open(path)
	if err != nil{
		// 错误处理
		return err
	}
	defer file.Close()
	// 打开这个文件
	all,err := ioutil.ReadAll(file)
	if err != nil{
		return err
	}
	writer.Write(all)
	return nil
}